package cn.hanyx.vlog.articleservice.service;

import cn.hanyx.vlog.articleservice.dto.ResponseObject;
import cn.hanyx.vlog.articleservice.entity.Article;
import cn.hanyx.vlog.articleservice.entity.User;
import cn.hanyx.vlog.articleservice.entity.Video;
import cn.hanyx.vlog.articleservice.feignclient.VideoFeignClient;
import cn.hanyx.vlog.articleservice.mapper.ArticleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * ArticleService
 *
 * @author hanyx
 * @date 2021/11/04
 */
@Slf4j
@Service
public class ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private VideoFeignClient videoFeignClient;

    public List<Article> list(User user) {
        int level;
        if (user != null && "vip".equals(user.getGrade())) {
            level = 2;
        } else {
            level = 1;
        }
        log.info("level" + level);
        List<Article> articleList = articleMapper.list(level);
        articleList.forEach(v -> {
            ResponseObject<Video> responseObject = videoFeignClient.findByArticleId(v.getArticleId());
            v.setVideo(responseObject.getData());
        });
        return articleList;
    }
}
